//
//  OwlSplitViewController.m
//  Owl
//
//  Created by Tianzhou on 09-2-11.
//  Copyright 2009 Tianzhou Software Inc.. All rights reserved.
//

#import "OwlSplitViewController.h"
#import "OwlWindowController.h"
#import "OwlSourceListViewController.h"
//#import "OwlTableViewController.h"
#import "OwlSplitRightPartViewController.h"
#import "OwlSourceListViewProtocol.h"

@interface OwlSplitViewController ()

//@property(assign,readwrite) OwlSourceListViewController *sourceListViewController;
//@property(assign,readwrite) OwlTableViewController *tableListViewController;

@end


@implementation OwlSplitViewController

- (OwlSourceListViewController *)sourceListViewController
{
	return [self.children objectAtIndex:0];
}

- (OwlSplitRightPartViewController *)rightTargetViewController
{
	return [self.children objectAtIndex:1];
}

- (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle windowController:(OwlWindowController *)windowController
{
	if (![super initWithNibName:name bundle:bundle windowController:windowController])
		return nil;
	
	// the represented object for the view controller is the document
	// TODO: Should move it to the super class init?
	[self setRepresentedObject:[windowController document]];
	OwlSourceListViewController *sourceListViewController = [[[OwlSourceListViewController alloc] initWithNibName:@"SourceList" bundle:nil windowController:windowController] autorelease];
	//OwlSplitLeftPartViewController  *leftTargetViewController = [[[OwlSplitLeftPartViewController alloc] initWithNibName:@"LeftView" bundle:nil windowController:windowController] autorelease];
	OwlSplitRightPartViewController *rightTargetViewController = [[[OwlSplitRightPartViewController alloc] initWithNibName:@"CustomView" bundle:nil windowController:windowController] autorelease];
	//OwlTableViewController *tableViewController = [[[OwlTableViewController alloc] initWithNibName:@"TableView" bundle:nil windowController:windowController] autorelease];
	//[self addChild:leftTargetViewController];
	[self addChild:sourceListViewController];
	// also set self as the source list view controller's observer
	[self addChild:rightTargetViewController];
	//[[self.children objectAtIndex:1] addChild:tableViewController];
	return self;
}

// -----------------------------------
// The tree has been built and all the objects in the NIB have received -init so we can then configure how the window will look.  Setting the subviews of the split view to the NSOutlineView and the NSTextView.
// ------------------------------------
- (void)awakeFromNib;
{
	[super awakeFromNib];
	self.view.subviews = [NSArray arrayWithObjects:[[self.children objectAtIndex:0] view],[[self.children objectAtIndex:1] view],nil];
	
	//[[self sourceListViewController] setSourceListObserver:self];
}

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context
{
//	if ([object isEqualTo:[[self sourceListViewController] sourceListController]]
//		&& [keyPath isEqualToString:@"selectedObjects"])
//	{
//		if ([[object selectedObjects] count])
//		{
//			ChildNode *selectedObject = [[object selectedObjects] objectAtIndex:0];
//			int i = [[selectedObject children] count];
//			// make sure the model responds before the view, so the view can
//			// get the updated model value.
//			if ([[self representedObject] conformsToProtocol:@protocol(OwlSourceListViewProtocol)])
//			{
//				[[self representedObject] respondToSourceListNewSelection:selectedObject];
//			}
//			
//			if ([[self rightTargetViewController] conformsToProtocol:@protocol(OwlSourceListViewProtocol)])
//			{
//				[[self rightTargetViewController] respondToSourceListNewSelection:selectedObject];
//			}
//			NSLog(@"");
//		}
//	}
}

@end
